<template>
    <div>
        <city-list :isCountry="this.isCountry" :isCity="this.isCity"></city-list>
        <div class="city-content">
            <card
                v-for="(item, index) in list"
                operate=0
                :title="item.title"
                type="city-list"
                :key="item.id"
                :bg="item.img"
                :countryId="item.countryId"></card>
        </div>
    </div>
</template>

<script>
import CityList from "@/components/CityList";
import Card from "@/components/Card";
import { Util } from "@/js/Util";
import { Config } from "@/js/Config";
import { AlertModule } from "vux";
export default {
  components: {
    CityList,
    Card,
    AlertModule
  },
  mounted() {
    this.setTitle();
    // 微信登录
    this.loginWechat(Util.getUrlParam("code"));
    this.getList();
  },
  methods: {
    setTitle() {
      document.title = "绝佳景色";
    },
    // 授权登录
    loginWechat(code) {
      // 判断是否有账号信息
      if (!sessionStorage.getItem("MeiJingKu_openid")) {
        if (!code) {
          location.href =
            "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
            Config._appid +
            "&r" +
            "edirect_uri=" +
            encodeURIComponent(location.href) +
            "&response_type=code&scope" +
            "=snsapi_userinfo&state=1#wechat_redirect";
        }
      } else {
        return false;
      }
      this.$vux.loading.show({ text: "Loading" });
      // 测试接口
      // this.$http.post("user/LoginByOpenid/v1?openid=1").then(response => {
      this.$http.post("user/LoginByCode/v1?code=" + code).then(response => {
          this.$vux.loading.hide();
          let result = response.data;
          if (!result.code) {
            sessionStorage.setItem("MeiJingKu_openid", result.data.openid);
          } else {
            location.href =
              "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
              Config._appid +
              "&redirect_uri=" +
              encodeURIComponent(location.href) +
              "&respon" +
              "se_type=code&scope=snsapi_userinfo&state=1#wechat_redirect";
          }
        },
        response => {
          Util.errorWarning(this);
        }
      );
    },
    getList() {
      this.$vux.loading.show({ text: "Loading" });
      this.$http.get("scenery/CountryList/v1").then(
        response => {
          this.$vux.loading.hide();
          let result = response.data;
          if (result.code) {
            this.$vux.toast.text(result.message, "bottom");
            return false;
          }
          for (let i in result.data) {
            let item = {
              countryId: result.data[i].id,
              title: result.data[i].name,
              img: result.data[i].picture
            };
            this.list.push(item);
          }
        },
        response => {
          Util.errorWarning(this);
        }
      );
    }
  },
  data() {
    return {
      isCountry: true,
      isCity: false,
      list: []
    };
  }
};
</script>

<style lang="less" scoped="scoped">
.city-content {
  padding-top: 5.5rem;
}
</style>